﻿

using IotContract.Dtos.Servers.Process;
using IotContract.Dtos.Servers.Pushs;
using IotContract.Enums;
using IotContract.Interfaces;
using IotContract.Interfaces.Process;
using QjServerProcess.Interfaces;
using QjServerProcess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XT.Common.Extensions;
using XT.Common.Services;

namespace QjServerProcess.DataProcess;
public class StackerDataProcess : IServerProcessHandle
{
    public string ProcessName { get; set; }= QjServerConst.ProcessName;
    public IotEquipType ProcessType { get; set; } = IotEquipType.STACKER;

    private readonly IServerMainManager _mainProcess;
    private readonly IProcessDataService _processDataService;
    private readonly ILogService _logService;

    public StackerDataProcess(IServerMainManager serverMainManager, IProcessDataService processDataService, ILogService logService)
    {
        _mainProcess = serverMainManager;
        _processDataService = processDataService;
        _logService = logService;
    }

    public async Task<bool> ParsingData()
    {
        try
        {
            // 获取数据
            var equips = await _mainProcess.GetEquips<PushSiemensS7Dto>(new IotContract.Models.Clients.ClientRequestCommand
            {
                RequestType = IotEquipType.STACKER.ToString(),
                ProtobufType = IotProtobufType.SIEMENS_S7.ToString()
            });


            var scUnitDic = _processDataService.ScUnits;
            // 堆垛机点位不需要设置UnitNum
            Parallel.ForEach(equips, equip =>
            {
                var tags = equip.Tags;
                ZdScUnitDto scUnit = null;
                if (scUnitDic.ContainsKey(equip.Num.To<int>()))
                {
                    scUnit = scUnitDic[equip.Num.To<int>()];
                }
                else
                {
                    scUnit = new ZdScUnitDto();
                    scUnit.EquipNum = equip.Num.To<int>();
                    scUnit.EquipId = equip.Id;
                    scUnit.Connection = equip.Connection;
                    scUnitDic.TryAdd(equip.Num.To<int>(), scUnit);
                }
                SetUnitTag(scUnit, tags);
            });
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        return true;
    }

    /// <summary>
    /// 取值
    /// </summary>
    /// <param name="scUnit"></param>
    /// <param name="tags"></param>
    private void SetUnitTag(ZdScUnitDto scUnit, List<PushEquipTag> tags)
    {
        scUnit.Tags = tags;
        Parallel.ForEach(scUnit.Tags, tag =>
        {
            int.TryParse(tag.Value, out int result);
            switch (tag.TagProperty)
            {
                case "ERROR":
                    {
                        scUnit.Error = result;
                        break;
                    }             
                case "MODE":
                    {
                        scUnit.Mode = result;
                        break;
                    }
                case "DEVICECODE":
                    {
                        scUnit.DeviceCode = result;
                        break;
                    }
                case "FRONT_TASK":
                    {

                        scUnit.FrontTask = result;
                        break;
                    }
                case "FRONT_COMMAND":
                    {
                        scUnit.FrontCommand = result;
                        break;
                    }
                case "FRONT_CARGO":
                    {
                        scUnit.FrontCargo = result;
                        break;
                    }
                case "FRONT_YERROR":
                    {
                        scUnit.FrontYError = result;
                        break;
                    }
                case "FRONT_Z":
                    {

                        scUnit.FrontZ = result;
                        break;
                    }
                case "FRONT_X":
                    {

                        scUnit.FrontX = result;
                        break;
                    }
                case "FRONT_Y":
                    {

                        scUnit.FrontY = result;
                        break;
                    }
                case "FRONT_ZERROR":
                    {
                        scUnit.FrontZError = result;
                        break;
                    }
                case "BACK_TASK":
                    {

                        scUnit.BackTask = result;
                        break;
                    }
                case "BACK_COMMAND":
                    {
                        scUnit.BackCommand = result;
                        break;
                    }
                case "BACK_CARGO":
                    {
                        scUnit.BackCargo = result;
                        break;
                    }
                case "BACK_YERROR":
                    {
                        scUnit.BackYError = result;
                        break;
                    }
                case "BACK_Z":
                    {

                        scUnit.BackZ = result;
                        break;
                    }
                case "BACK_X":
                    {

                        scUnit.BackX = result;
                        break;
                    }
                case "BACK_Y":
                    {

                        scUnit.BackY = result;
                        break;
                    }
                case "BACK_ZERROR":
                    {
                        scUnit.BackZError = result;
                        break;
                    }

            }
        });
    }

    public async Task<bool> ProcessData()
    {
        return true;
    }

    public async Task<bool> ProcessOrder()
    {
        return true;
    }

    public async Task<bool> ProcessWarn()
    {
        return true;
    }

    public bool Stop()
    {
        _processDataService.ScUnits.Clear();
        return true;
    }
}
